//
//  UIBarButtonItem+YC.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/26.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "UIBarButtonItem+YC.h"

@implementation UIBarButtonItem (YC)


+ (UIBarButtonItem *)itemWithImage:(NSString *)image highlightImage:(NSString *)highlightImage target:(id)target action:(SEL)sel
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highlightImage] forState:UIControlStateHighlighted];
    [button sizeToFit];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    
    //把button包装到uivew中,处理点击范围变大的问题
    UIView *barButtonItemView = [[UIView alloc] initWithFrame:button.bounds];
    [barButtonItemView addSubview:button];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:barButtonItemView];
    
    return barButton;
}

+ (UIBarButtonItem *)itemWithImage:(NSString *)image selectedImage:(NSString *)selectedImage target:(id)target action:(SEL)sel
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
    [button sizeToFit];
    
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    
    //把button包装到uivew中,处理点击范围变大的问题
    UIView *barButtonItemView = [[UIView alloc] initWithFrame:button.bounds];
    [barButtonItemView addSubview:button];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:barButtonItemView];
    
    return barButton;
    
    
}

+ (UIBarButtonItem *)backItemWithImage:(NSString *)image highlightImage:(NSString *)highlightImage target:(id)target action:(SEL)sel title:(NSString *)title
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highlightImage] forState:UIControlStateHighlighted];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    //设置button尺寸
    [button sizeToFit];
    //让它内容内边距向左偏移20,美观
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    button.imageView.contentMode = UIViewContentModeLeft;
    //把button包装到uivew中,处理点击范围变大的问题
    button.yc_width = 30;
    button.yc_height = 44;
    UIView *barButtonItemView = [[UIView alloc] initWithFrame:button.frame];
    [barButtonItemView addSubview:button];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:barButtonItemView];
    return leftItem;
}


@end
